package middol.sys

import grails.gorm.services.Service
import grails.gorm.transactions.Transactional
import middol.result.ResultUtils
import middol.utils.DomainUtils

/**
* @Description:    菜单信息服务类
* @Author:         zhangjiayu@middol.com
* @CreateDate:     2020/10/28 19:21
* @Version:        1.0
*/
class PermissionService {
    PermissionDataService permissionDataService
    /**
     * 获取所有菜单
     * @param record
     * @return
     */
    def getAllPermission(def record) {
        def list = permissionDataService.list([sort: "sortNo"])
        def parentPermission = null
        if(record?.orgId){
            parentPermission = permissionDataService.get(record?.permissionId)
        }
        def orgList = getPermissionChildren(DomainUtils.domainListToMapList(list), parentPermission)
        return ResultUtils.ok(orgList)
    }

    /**
     * 获取所有子部门信息
     * @param orgList
     * @param org
     * @return
     */
    def getPermissionChildren(List permissionList, Object permission = null){
        def newOrgList = permissionList.findAll {Map it ->
            it.parentPermission?.id == permission?.id
        }
        newOrgList.each {Map it ->
            it?.children = getPermissionChildren(permissionList, it)
        }
        return newOrgList
    }
}
